//
//  AppDelegate.swift
//  微博天下
//
//  Created by Tsz on 15/10/8.
//  Copyright © 2015年 Tsz. All rights reserved.
//

import UIKit

//试图控制器切换通知字符串
let TSZRootViewControllerSwitchNotification = "TSZRootViewControllerSwitchNotification"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

//    AFNetworkActivityIndicatorManager.sharedManager.set
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //开启数据库
        TSZSQLiteManager.shareManager
        
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "checkViewController:", name: TSZRootViewControllerSwitchNotification, object: nil)
        
        
       //设置全局的颜色
        setInitColor()
        
        //显示联网状态
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        //指定跟控制器
//        window?.rootViewController = MainViewController()
//        window?.rootViewController = TSZWelcomeViewController()
//        window?.rootViewController = TSZNewFeatureCollectionViewController()
        
        window?.rootViewController = defaultViewController()
        window?.makeKeyAndVisible()
        
        return true
    }
    
    //程序进入后台就清除缓存数据
    func applicationDidEnterBackground(application: UIApplication) {
        //清除数据
        TSZStatusDAL.clearDatabaseCache()
    }
    
    
    //切换控制器
    func checkViewController(notify: NSNotification){
        
//        print("切换控制器\(notify)")
        
        let mainVc = notify.object as! Bool
        //如果返回是yes 就直接到主界面 ，注册界面 ， 否则就是欢迎界面
        window?.rootViewController = mainVc ? MainViewController() : TSZWelcomeViewController()
    }
    
    //返回启动默认的控制器
    private func defaultViewController() ->UIViewController{
        /**
        1、判断是否登录
        */
        if !TSZUserAcountModel.userLogin{
            return MainViewController()
        }
        //2、判断是不是新版本
        return isNewVersion() ? TSZNewFeatureCollectionViewController() : TSZWelcomeViewController()
    }
    
    //判断有木有新版本
    private func isNewVersion() ->Bool {
        //1、获取程序当前的版本
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        //2、获取 之前 的版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        //3、将当前版本保存到偏好设置
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        
        //同步
        NSUserDefaults.standardUserDefaults().synchronize()
        //4、返回比较结果
        return currentVersion > sandboxVersion
    }
    
    //设置全局的设置
    private func setInitColor(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

